//
//  CSWPushViewController.m
//  彩票
//
//  Created by xiaomage on 15/9/24.
//  Copyright © 2015年 xiaomage. All rights reserved.
//

#import "CSWPushViewController.h"

#import "CSWGroupItem.h"
#import "CSWSettingArrowItem.h"
#import "CSWSettingCell.h"
@interface CSWPushViewController ()
// 保存当前tableView有多少组,元素应该是一个groupItem
@property (nonatomic, strong) NSMutableArray *groups;


@end

@implementation CSWPushViewController

- (NSMutableArray *)groups
{
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加第0组
    [self setUpGroup0];
    
}
// 添加第0组
- (void)setUpGroup0
{
    // 1.创建组模型
    CSWGroupItem *group = [[CSWGroupItem alloc] init];
    
    // 2.创建行模型
    CSWSettingArrowItem *item = [CSWSettingArrowItem itemWithImage:nil title:@"开奖推送"];
    CSWSettingArrowItem *item1 = [CSWSettingArrowItem itemWithImage:nil title:@"比分直播"];
    item1.operationBlock = ^(){
        NSLog(@"0比0");
    };
    CSWSettingArrowItem *item2 = [CSWSettingArrowItem itemWithImage:nil title:@"开奖推送"];
    CSWSettingArrowItem *item3 = [CSWSettingArrowItem itemWithImage:nil title:@"开奖推送"];

    group.items = @[item,item1,item2,item3];
    [self.groups addObject:group];
}


#pragma mark--数据源方法
- (NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView
{
    return self.groups.count;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 获取对应的组模型
    CSWGroupItem *group = self.groups[section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    // 创建cell
    CSWSettingCell *cell = [CSWSettingCell cellWithTableView:tableView];
    CSWGroupItem *group = self.groups[indexPath.section];
    
    CSWSettingItem *item = group.items[indexPath.row];
    cell.item = item;
    
    return cell;
}

#pragma mark - tableView代理
- (NSString *)tableView:(nonnull UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // 获取当前组模型
    CSWGroupItem *group = self.groups[section];
    return group.headTitle;
    
}
//  返回每一组的尾部标题
- (NSString *)tableView:(nonnull UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    // 获取当前组模型
    CSWGroupItem *group = self.groups[section];
    return group.footTitle;
    
}

// 只要点击cell就会调用
- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    CSWGroupItem *group = self.groups[indexPath.section];
    
    CSWSettingItem *item = group.items[indexPath.row];
    
    if (item.operationBlock) {
        // 调用block
        item.operationBlock();
        return;
    }
    
    if ([item isKindOfClass:[CSWSettingArrowItem class]]) {
        // 标示箭头类型,可以跳转
        CSWSettingArrowItem *arrowItem = (CSWSettingArrowItem *)item;
        // 获取需要跳转控制器的类名
        if (arrowItem.descVc) {
            
            UIViewController *vc =   [[arrowItem.descVc alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
}

@end
